package com.example.play.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.common.entity.SimpleVideoEntity
import com.example.image.glide.GlideImgLoader
import com.example.play.databinding.ItemVideoPlayLayoutBinding

class SimpleVideoListAdapter constructor(private val list: List<SimpleVideoEntity>) : RecyclerView.Adapter<SimpleVideoListAdapter.ItemViewHolder>(){
    private var itemClickListener: ItemClickListener? = null

    override fun getItemCount(): Int = list.size

    inner class ItemViewHolder(val dataBinding: ItemVideoPlayLayoutBinding): RecyclerView.ViewHolder(dataBinding.root)

    interface ItemClickListener{
        fun onItemChildClick(v: View, position: Int, entity: SimpleVideoEntity)
    }

    fun setOnItemClickListener(l: ItemClickListener){
        itemClickListener = l
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        return ItemViewHolder(ItemVideoPlayLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val entity = list[position]
        GlideImgLoader.loadCornerImage(holder.dataBinding.root.context, entity.videomainimag, holder.dataBinding.ivMovieImg, 20)
        holder.dataBinding.apply {
            tvMovieDesc.text = entity.description
            tvPlayCount.text = "${entity.playnum}次播放"
        }

        //设置点击事件
        holder.dataBinding.root.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position, entity)
        }

    }

}